<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="合同信息">
              <j-search-select-tag placeholder="请选择合同信息" v-model="queryParam.leaseContractId"
                                   :async="true"  dict="zcgl_lease_contract_info,contract_num,id" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户信息">
              <j-search-select-tag placeholder="请选择客户信息" v-model="queryParam.customerId"
                                   :async="true"   dict="zcgl_customer_info,name,id" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="资产信息">
              <j-search-select-tag placeholder="请选择资产信息" v-model="queryParam.assetId"
                                   :async="true"    dict="zcgl_asset_info,asset_name,id" />
            </a-form-item>
          </a-col>

          <!--          <a-col v-show="false" :xl="6" :lg="7" :md="8" :sm="24">-->
          <!--            <a-form-item label="支付状态">-->
          <!--              <a-input placeholder="请选择支付状态" v-model="queryParam.payStatus"/>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <template v-if="toggleSearchStatus">
            <!--            <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
            <!--              <a-form-item label="支付方式">-->
            <!--                <j-dict-select-tag title="支付方式" placeholder="请选择支付方式" v-model="queryParam.payType" dictCode="pay_type"/>-->
            <!--              </a-form-item>-->
            <!--            </a-col>-->
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="管理人员">
                <j-search-select-tag placeholder="请选管理人员" :async="true" v-model="queryParam.managerId"
                                     dict="sys_user,realname,id" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="管区">
                <j-dict-select-tag placeholder="请选管区" :async="true" v-model="queryParam.regional"
                                   dictCode="zcgl_regional" />
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否确认">
                <j-dict-select-tag placeholder="请选择是否确认" v-model="queryParam.amountConfirmation" dictCode="yn" />
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否提醒">
                <j-dict-select-tag placeholder="请选择是否提醒" v-model="queryParam.isRemind" dictCode="yn" />
              </a-form-item>
            </a-col>

            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="开始日期">
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.startTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.startTime_end"></j-date>
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-is-group>
              <a-form-item label="入账公司">
                <j-company-select-tag placeholder="请选择入账公司" v-model="queryParam.rentCompanyId"/>
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-is-group>
              <a-form-item label="所属公司">
                <j-company-select-tag placeholder="请选择所属公司" v-model="queryParam.companyId"/>
              </a-form-item>
            </a-col>
            <!--            <a-col :xl="10" :lg="11" :md="12" :sm="24">-->
            <!--              <a-form-item label="收款日期">-->
            <!--                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.receiptDate_begin"></j-date>-->
            <!--                <span class="query-group-split-cust"></span>-->
            <!--                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.receiptDate_end"></j-date>-->
            <!--              </a-form-item>-->
            <!--            </a-col>-->
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'receivablesInfoUn:add'" @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button v-has="'receivablesInfoUn:export'" type="primary" icon="download" @click="handleExportXls('应收款项（待收）信息')">导出
      </a-button>
      <a-upload v-has="'receivablesInfoUn:import'" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button v-has="'receivablesInfoUn:temp'" type="primary" icon="download"
                @click="handleExportXlsTemplate('应收款项信息模板')">模板下载
      </a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item v-has="'receivablesInfoUn:delete'" key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
<!--          <a-menu-item v-has="'receivablesInfoUn:reminder'" key="2" @click="handlePaymentReminderBatch">-->
<!--            <a-icon type="message" />-->
<!--            缴费提醒-->
<!--          </a-menu-item>-->
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :scroll="{ x: 2000, y: screenHeight}"
        class="j-table-force-wrap"
        @change="handleTableChange">
        <!--        , getCheckboxProps: getCheckboxProps-->
        <!--        :rowSelection="rowSelection"-->

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">

          <a v-has="'receivablesInfoUn:com'" v-show="record.amountConfirmation == 0"
             @click="handleAmountConfirmation(record)">金额确认</a>
          <a-divider v-has="'receivablesInfoUn:com'" v-show="record.amountConfirmation == 0" type="vertical" />

          <span v-if="record.payStatus == 10 || record.payStatus == null">

            <a v-has="'receivablesInfoUn:toll'" v-show="record.amountConfirmation == 1" @click="handleOperation('收费', record, 1)">收费</a>
            <a-divider v-has="'receivablesInfoUn:toll'" v-show="record.amountConfirmation == 1" type="vertical" />

            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
<!--                <a-menu-item v-has="'receivablesInfoUn:reminder'" v-show="record.amountConfirmation == 1">-->
<!--                   <a @click="handlePaymentReminder(record)">提醒缴费</a>-->
<!--                </a-menu-item>-->

                <template v-if="isApproval != 1">
                  <a-menu-item v-has="'receivablesInfoUn:edit'">
                    <a @click="handleEdit(record)">编辑</a>
                  </a-menu-item>

                  <a-menu-item v-has="'receivablesInfoUn:demolition'">
                    <a @click="handleOperation('拆费', record, 2)">拆费</a>
                  </a-menu-item>
                </template>

                <template v-else>
                  <a-menu-item v-if="record.amountConfirmation == 0" v-has="'receivablesInfoUn:edit'">
                    <a @click="handleEdit(record)">编辑</a>
                  </a-menu-item>
                  <a-menu-item v-else v-has="'receivablesInfoUn:edit'">
                    <a @click="demolitionFeeForm(record,'变更', 1)">变更申请</a>
                  </a-menu-item>
                  <a-menu-item v-has="'receivablesInfoUn:demolition'">
                    <a @click="demolitionFeeForm(record,'拆费', 2)">拆费申请</a>
                  </a-menu-item>
                </template>

                <a-menu-item v-has="'receivablesInfoUn:delete'">
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </span>

      </a-table>
    </div>

    <receivablesInfo-modal ref="modalForm" @ok="modalFormOk"></receivablesInfo-modal>
    <receivablesOperation-modal ref="modalOperationForm" @ok="modalOperationOk"></receivablesOperation-modal>
    <amountConfirmation-modal ref="amountConfirmationForm" @ok="modalOperationOk"></amountConfirmation-modal>

    <!--流程表单-->
    <a-modal :title="lcModa.title" v-model="lcModa.visible" :footer="null" :maskClosable="false" width="80%">
      <component :disabled="lcModa.disabled" v-if="lcModa.visible" :is="lcModa.formComponent"
                 :processData="lcModa.processData" :isNew="lcModa.isNew"
                 @afterSubmit="processFormOk" @close="lcModa.visible=false, lcModa.disabled = false"></component>
    </a-modal>
  </a-card>
</template>


<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ReceivablesInfoModal from './modules/ReceivablesInfoModal'
import ReceivablesOperationModal from './modules/ReceivablesOperationModal'
import AmountConfirmationModal from './modules/AmountConfirmationModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JDate from '@/components/jeecg/JDate.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import { paymentReminder } from '@/api/receivables'
import { buildTableMethod, buildTableData } from '@/api/receivablesInfoData'
import { activitiMixin } from '@views/activiti/mixins/activitiMixin'
import JCompanySelectTag from '@comp/jeecg/JCompanySelectTag'

export default {
  name: 'ReceivablesInfoList',
  mixins: [activitiMixin, JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    JDate,
    JSearchSelectTag,
    JCompanySelectTag,
    ReceivablesOperationModal,
    AmountConfirmationModal,
    ReceivablesInfoModal
  },

  data () {
    return buildTableData({

      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          fixed: "left",
          customRender: (text, record, index) => `${(this.ipagination.current - 1) * this.ipagination.pageSize + index + 1}`

        },
        {
          title:'合同信息',
          align:"center",
          dataIndex: 'leaseContractId_dictText'
        },
        {
          title:'资产信息',
          align:"center",
          dataIndex: 'assetId_dictText'
        },
        {
          title: '资产明细',
          align: 'center',
          // fixed:"left",
          // width: 200,
          dataIndex: 'assetSubIds_dictText'
        },
        {
          title:'客户信息',
          align:"center",
          dataIndex: 'customerId_dictText'
        }, {
          title: '付款开始日期',
          align: 'center',
          dataIndex: 'payStartDate'
        },
        {
          title:'款项说明',
          align:"center",
          dataIndex: 'paymentInstructions_dictText'
        },
        {
          title:'应收金额',
          align:"center",
          dataIndex: 'amountMoney',
          customRender: function (text) {
            if (!text) {
              text = 0
            }
            return text.toLocaleString("zh-Hans-CN",{style:"currency",currency:"CNY"});
          }
        },
        {
          title:'已收金额',
          align:"center",
          dataIndex: 'paidAmountMoney',
          customRender: function (text) {
            if (!text) {
              text = 0
            }
            return text.toLocaleString("zh-Hans-CN",{style:"currency",currency:"CNY"});
          }
        },
        {
          title:'未收金额',
          align:"center",
          dataIndex: 'unAmountMoney',
          customRender: function (text) {
            if (!text) {
              text = 0
            }
            let te =  text.toLocaleString("zh-Hans-CN",{style:"currency",currency:"CNY"});
            return (<label style="color: red;">{te}</label>)
          }
        },
        {
          title:'是否确认',
          align:"center",
          dataIndex: 'amountConfirmation_dictText',
          hidden:true,
          customRender: function (t, r, index) {
            if (r.amountConfirmation == 1) {
              return t + '（' + r.amountConfirmationTime + '）'
            }
            return t
          }
        },
        {
          title:'入账公司',
          align:"center",
          dataIndex: 'rentCompanyId_dictText',
        },
        {
          title:'款项周期',
          align:"center",
          dataIndex: 'startTime',
          width: 250,
          customRender:function (text, r, index) {
            // let startTime = !text?"":(text.length>10?text.substr(0,10):text)
            // let endTime = !r.endTime?"":(r.text.endTime>10?r.endTime.substr(0,10):r.endTime)
            return text + "至" + r.endTime
          }
        },
        {
          title:'备注',
          align:"center",
          dataIndex: 'remark'
        },
        {
          title: '附件',
          align: 'center',
          dataIndex: 'attachment',
          scopedSlots: { customRender: 'fileSlot' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:200,
          scopedSlots: { customRender: 'action' }
        }
      ],
      description: '应收款项信息管理页面',
      queryParam: {
        payStatus: 10
      },
      isorter: {
        column: 'payStartDate',
        order: 'ASC'
      },
      props: {
        model: Array
      }
    })
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },

  methods: buildTableMethod({

    getCheckboxProps: record => ({
      props: {
        // 全部默认禁止选中
        // disabled: true,
        // 某几项默认禁止选中(R: 当state等于1时)
        disabled: record.amountConfirmation == 0
      }
    }),
    handleAmountConfirmation (record) {
      this.$refs.amountConfirmationForm.show(record)
    },
    initDictConfig () {
      this.toTop = 485
      let height = window.innerHeight - this.toTop
      if (height < 300) {
        height = 300
      }
      this.screenHeight = height
      this.addColumns()
    },

    handlePaymentReminderBatch () {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        let ids = ''
        for (let a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        let that = this
        this.$confirm({
          title: '确认提醒',
          content: '是否提醒选中的收费信息?',
          onOk: function () {
            that.paymentReminder(ids)
          }
        })
      }
    },
    handlePaymentReminder (record) {
      this.paymentReminder(record.id)
    },
    paymentReminder (ids) {
      let that = this
      paymentReminder({ ids: ids }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      }).finally(() => {
        // that.close()
      })
    },
    //自己的方法
    searchReset () {
      this.queryParam = { payStatus: 10 }
      this.loadData(1)
    },
    demolitionFeeForm (r, name, type) {
      let tableData = {
        routeName: '@/views/activiti/form/ReceivablesDemolitionFee',
        tableName: 'zcgl_receivables_info',
        tableId: r.id
      }
      if (type == 1){
        tableData.routeName = "@/views/activiti/form/ReceivablesEdit"
      }
      this.processCreateCom(tableData, '发起' + name + '申请流程', r.processStatus)
    }
    //
    // demolitionFeeFormOk(){
    //   // this.loadData()
    //   this.lcModa.visible = false;
    //   this.toApplyListUrl()
    // }
  })
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>